package com.yycwxt.modules.admin.cwfy;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yycwxt.domain.Cwfy;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Mapper
public interface CwfyMapper extends BaseMapper<Cwfy> {

    @Select("""
            select  account_date 账期, shop_name 店铺名称
                        
                 , sum(if (subject_code = '9001-01-002', cost, null)) 本月销售
            	 , sjxsje/(SELECT SUM(sjxsje)FROM imp_gdpxstj ig
            	 JOIN glb_shop s ON ig.dp = s.`name` WHERE zq = 202208
            	 AND CURRENT_TIMESTAMP > endmoneth) 销售占比1
            	 , sjxsje/(SELECT SUM(sjxsje)FROM imp_gdpxstj ig
            	 JOIN glb_shop s ON ig.dp = s.`name` WHERE zq = 202208
            	 AND type in (1,2) AND CURRENT_TIMESTAMP > endmoneth) 销售占比2
            	 , sjxsje/(SELECT SUM(sjxsje)FROM imp_gdpxstj ig
            	 JOIN glb_shop s ON ig.dp = s.`name`
            	 WHERE zq = 202208 AND tax = 1) 销售占比3
                       
            	 , sum(if (subject_code = '1001-01-006', cost, null)) 税金
                 , sum(if (subject_code = '1001-01-007', cost, null)) 增值税
                        
                 , sum(if (subject_code = '1001-01-001-01', cost, null)) 总部工资
                 , sum(if (subject_code = '1001-01-001-02', cost, null)) 总部运营工资
                 , sum(if (subject_code = '1001-01-001-03', cost, null)) 总部抖音工资
                 , sum(if (subject_code = '1001-01-001-04', cost, null)) 总部伍拾佣工资
                 , sum(if (subject_code = '1001-01-005', cost, null)) 抖音工资
                 , sum(if (subject_code = '1003-01-011', cost, null)) 机器人软件
                 , sum(if (subject_code = '1007-01-003', cost, null)) 低值易耗品
                 , sum(if (subject_code = '1005-01-003', cost, null)) 到货运杂费
                        
                 , sum(if (subject_code = '1001-01-002-01', cost, null)) 总部社保
                 , sum(if (subject_code = '1001-01-002-02', cost, null)) 总部运营社保
                 , sum(if (subject_code = '1001-01-002-03', cost, null)) 总部抖音社保
                 , sum(if (subject_code = '1001-01-002-04', cost, null)) 抖音社保
                        
                 , sum(if (subject_code = '1001-01-003', cost, null)) 工会经费
                 , sum(if (subject_code = '1002-01-003', cost, null)) 办公费
                 , sum(if (subject_code = '1001-02-001-01', cost, null)) 总部房租
                 , sum(if (subject_code = '1001-02-001-02', cost, null)) 抖音房租
                        
                 , sum(if (subject_code = '1008-01-003', cost, null)) 水电费
                 , sum(if (subject_code = '1008-01-002', cost, null)) 固定资产
                 , sum(if (subject_code = '1001-01-004', cost, null)) 其他费用
                        
                 , sum(if (subject_code = '1002-01-001', cost, null)) 差旅费
                 , sum(if (subject_code = '1002-01-002', cost, null)) 车辆费
                 , sum(if (subject_code = '1002-01-003', cost, null)) 办公费
                 , sum(if (subject_code = '1002-01-004', cost, null)) 团建费
                 , sum(if (subject_code = '1002-01-005', cost, null)) 业务招待费
                 , sum(if (subject_code = '1002-01-006', cost, null)) 交通费
                 , sum(if (subject_code = '1002-01-007', cost, null)) 招聘费
                 , sum(if (subject_code = '1002-01-008', cost, null)) 抖音直播器材食材
                 , sum(if (subject_code = '1002-01-009', cost, null)) 样品费
                        
                 , sum(if (subject_code = '1004-01-010', cost, null)) 广告推广费SD佣金
                 , sum(if (subject_code = '1004-01-019', cost, null)) 广告推广费大家问
                 , sum(if (subject_code = '1004-01-020', cost, null)) 广告推广费评价服务费
                 , sum(if (subject_code = '1004-01-021', cost, null)) 广告推广费魔讯
                        
                 , sum(if (left(subject_code, 11) = '1003-01-024', cost * subject_direction, null)) 线下退款
                 , sum(cost * subject_direction) 合计
            from core_flows fl
            JOIN imp_gdpxstj ig on fl.shop_name = ig.dp
            JOIN glb_shop g on ig.dp = g.`name`
            WHERE account_date = #{account_date}
            group by account_date, shop_name,销售占比1,销售占比2,销售占比3;
            """)
    List<Map<String, Object>> getcwfy(int zq);

    // 下载源文件
    @Select("select url from imp_files where remark like '%财务费用%'")
    List<String> getCwfyUrl();

    @Select("call impCwfy(" +
            "#{pzq,mode=IN,jdbcType=INTEGER}, " +
            "#{pkm,mode=IN,jdbcType=VARCHAR}, #{pje,mode=IN,jdbcType=DECIMAL}, #{pftlx,mode=IN,jdbcType=INTEGER})"
    )
    public void impCwfy(
            Integer pzq,
            String pkm, BigDecimal pje, Integer pftlx
    );
}
